package com.example.demo.OtherCoding;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-05-12
 * Time: 20:32
 */
public class Code_sortedArrayToBST {

    class Solution {
        private TreeNode dfs(int[] nums, int left, int right) {
            if (left > right) {
                return null;
            }
            int mid = (left + right) / 2;
            TreeNode root = new TreeNode(nums[mid]);
            root.left = dfs(nums, left, mid - 1);
            root.right = dfs(nums, mid + 1, right);
            return root;
        }

        public TreeNode sortedArrayToBST(int[] nums) {
            //中序遍历
            //左根右
            return dfs(nums, 0, nums.length - 1);
        }
    }
}
